package com.example.day0525.news

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.base.entity.NewsBean
import com.example.base.entity.NewsBeanItem
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import com.example.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class NewsViewModel @Inject constructor(val apiService: ApiService):BaseViewModel() {
    fun getNews():Flow<PagingData<NewsBeanItem>>{
        return Pager(PagingConfig(pageSize = 10), pagingSourceFactory = {
            NewsDataSouse(apiService)
        }).flow.cachedIn(viewModelScope)

    }
    val success =MutableLiveData<String?>()
    fun delete(id:Int){
        httQuest({apiService.delete(id)},success::postValue)
    }
    val newSuccess =MutableLiveData<NewsBean>()
    fun getNew(){
        httQuest({apiService.getNews()},newSuccess::postValue)
    }
}